<%--
  Created by IntelliJ IDEA.
  User: LC
  Date: 2019/1/17
  Time: 20:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<link rel="stylesheet" href="/static/layui/css/layui.css">
<link rel="stylesheet" href="/static/dtree/dtree.css">
<link rel="stylesheet" href="/static/dtree/font/dtreefont.css">
<style>
    .site-btn {
        position: absolute;
        bottom: 15px;
        right: 20px;
    }
</style>
<head>
    <title>Title</title>
</head>
<body>
<div style="height: 500px;overflow: auto;">
    <div class="dialog-tree-div">
        <div class="dialog-show-tree">
            <ul id="openTree" class="dtree" data-id="0"></ul>
        </div>
    </div>
</div>
</body>
</html>
<script src="/static/layui/layui.js"></script>
<script>
    layui.extend({
        dtree: '/static/dtree/dtree'
    }).use(['element','layer', 'table', 'code' ,'util', 'dtree'], function(){
        var element = layui.element,
            layer = parent.layer === undefined ? layui.layer : top.layer
        table = layui.table,
            util = layui.util,
            dtree = layui.dtree,
            $ = layui.$;
        dtree.render({
            elem: "#openTree",
            url: "/api/per/roleList?id=${roleId}",
            method:'GET',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            dataFormat: "list",  //配置data的风格为list
            response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
            checkbar: true,
            checkbarType: "p-casc", // 默认就是all，其他的值为： no-all  p-casc   self  only
            skin: "mine",  // 自定义风格
            dot: false  // 隐藏小圆点
        });
        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        })

        // 点击节点触发回调
        // dtree.on("node('openTree')" ,function(obj){
        //     layer.msg(JSON.stringify(obj.param));
        //     var index = parent.layer.getFrameIndex(window.name);
        //     parent.layer.close(index);
        // });
        dtree.on("nodedblclick('openTree')" ,function(obj){
            console.log(obj.param); // 点击当前节点传递的参数
            console.log(obj.childParams); // 当前节点的所有子节点参数
        });
        $('#getNodes').click(function () {
            var params = dtree.getCheckbarNodesParam("openTree");
            layer.msg(JSON.stringify(params));
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });

    })
    function get() {
        var params = dtree.getCheckbarNodesParam("openTree");
        var newPer = [];
        var pers = [];
        for(var i = 0;i<params.length;i++){
            newPer.push(params[i].nodeId);
            pers.push(params[i].nodeId);
        }
        var oldPer = ${permissions};
        if(newPer.length>=oldPer.length){
            for(var i = 0;i<oldPer.length;i++){
                var index = newPer.indexOf(oldPer[i]+"");
                console.log(index);
                newPer.splice(index,1)
            }
        }else if(newPer.length>oldPer.length){
            for(var i = 0;i<oldPer.length;i++){
                var index = oldPer.indexOf(newPer[i]+"");
                console.log(index);
                oldPer.splice(index,1)
            }
        }
        if(newPer.length == 0 || oldPer.length.length == 0){
            return 0;
        }else{
            $.ajax({
                url: '/api/role/update',
                traditional: true,
                data:{"roleId":${roleId},"pers":pers},
                type: 'post',
                success: function (res) {
                    if(res.data == 1){
                        parent.layer.msg("修改成功！",{icon: 1})
                        return 1;
                    }else{
                        parent.layer.msg("修改失败！",{icon: 2})
                        return -1;
                    }
                }
            });
        }
    };
</script>
